home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1992 …SCII & the Runetime Code / ADC Developer CD (1992-07) (''Butch ASCII And The Runtime Code'')_iso / Dev.CD 199207.iso / Developer Essentials / DTS Sample Code / System 7.0 Samples / MacShell / File2.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-12-04  |  6.2 KB  |  288 lines  |  [TEXT/MPS ]

  1. /*
  2. ** Apple Macintosh Developer Technical Support
  3. **
  4. ** Program:     MacShell
  5. ** File:        file2.c
  6. ** Written by:  Eric Soldan
  7. **
  8. ** Copyright © 1990-1991 Apple Computer, Inc.
  9. ** All rights reserved.
  10. */
  11.  
  12.  
  13.  
  14. /*****************************************************************************/
  15.  
  16.  
  17.  
  18. #include "MacShell.h"            /* Get the MacShell includes/typedefs, etc.    */
  19. #include "MacShellCommon.h"        /* Get the stuff in common with rez.        */
  20. #include "MacShell.protos"        /* Get the prototypes for MacShell.            */
  21.  
  22. #ifndef __ERRORS__
  23. #include <Errors.h>
  24. #endif
  25.  
  26. #ifdef THINK_C
  27. #include "Utilities.h"
  28. #else
  29. #ifndef __UTILITIES__
  30. #include <Utilities.h>
  31. #endif
  32. #endif
  33.  
  34.  
  35.  
  36. /*****************************************************************************/
  37. /*****************************************************************************/
  38.  
  39.  
  40.  
  41. /* AppFreeDocument
  42. **
  43. ** Frees up any application-specific memory in the document.  The document
  44. ** may have a bunch of handles off the main handle of the document.  This
  45. ** is where they are freed.  AppDisposeDocument calls this prior to releasing
  46. ** the ram for the main handle of the document, so release everything else
  47. ** here, or you will have a memory leak.
  48. */
  49.  
  50. #pragma segment File
  51. void    AppFreeDocument(FileRecHndl frHndl)
  52. {
  53. #if MACSHELL_VERSION
  54.  
  55.     Handle    textHndl;
  56.  
  57.     if (textHndl = (*frHndl)->doc.textHndl) {
  58.         DisposHandle(textHndl);
  59.         (*frHndl)->doc.textHndl = nil;
  60.     }
  61.  
  62. #else
  63. #pragma unused (frHndl)
  64. #endif
  65.  
  66. }
  67.  
  68.  
  69.  
  70. /*****************************************************************************/
  71.  
  72.  
  73.  
  74. /* AppInitDocument
  75. **
  76. ** Do any additional document initialization here.
  77. */
  78.  
  79. #pragma segment File
  80. OSErr    AppInitDocument(FileRecHndl frHndl)
  81. {
  82.  
  83. #ifdef MACSHELL_VERSION
  84.  
  85.     (*frHndl)->connect.connected = false;
  86.     (*frHndl)->doc.version       = kVersion;
  87.     (*frHndl)->doc.printRecValid = false;
  88.  
  89. #endif
  90.  
  91. #if MACSHELL_VERSION
  92.  
  93.     (*frHndl)->doc.textHndl = nil;
  94.  
  95. #endif
  96.  
  97.     return(noErr);
  98. }
  99.  
  100.  
  101.  
  102. /*****************************************************************************/
  103.  
  104.  
  105.  
  106. /* AppReadDocument
  107. **
  108. ** Reads in specified file into the data portion of a document handle.
  109. */
  110.  
  111. #pragma segment File
  112. OSErr    AppReadDocument(FileRecHndl frHndl)
  113. {
  114.     short    fileRefNum;
  115.     OSErr    err;
  116. #ifdef MACSHELL_VERSION
  117.     char    hstate;
  118.     Ptr        ptr1, ptr2;
  119.     long    count;
  120. #endif
  121. #if MACSHELL_VERSION
  122.     Handle    textHndl;
  123. #endif
  124.  
  125.     fileRefNum = (*frHndl)->fileState.refNum;
  126.  
  127.     /* This sample code section is separated from the below section because
  128.     ** you may want to keep this sample, whereas you probably want to replace
  129.     ** the next part.  The general print and version information is read in
  130.     ** here, and the way that it is handled works well enough that you probably
  131.     ** want to keep it.  The next section however, is very application specific.
  132.     ** Also see AppWriteDocument. */
  133.  
  134.     err = SetFPos(fileRefNum, fsFromStart, 0);
  135.         /* Set the file position to the beginning of the file. */
  136.  
  137. #ifdef MACSHELL_VERSION
  138.  
  139.     if (!err) {        /* Read header info from file. */
  140.         hstate = LockHandleHigh((Handle)frHndl);
  141.         ptr1   = (Ptr)&((*frHndl)->doc);
  142.         ptr2   = (Ptr)&((*frHndl)->doc.endVersPrintInfo);
  143.         count  = (long)ptr2 - (long)ptr1;
  144.         err    = FSRead(fileRefNum, &count, ptr1);
  145.         HSetState((Handle)frHndl, hstate);
  146.         if ((*frHndl)->doc.version != kVersion) err = kWrongVersion;
  147.     }
  148.  
  149. #endif
  150.  
  151. #if MACSHELL_VERSION
  152.  
  153.     if (!err) {        /* Read TextEdit text from file. */
  154.         textHndl = NewHandle(32768);
  155.         if (!(err = MemError())) {
  156.             count = 32768;
  157.                 /* The size of the text isn't saved to disk.  This is the maximum
  158.                 ** that a TextEdit record can accept. */
  159.             hstate = LockHandleHigh(textHndl);
  160.             err    = FSRead(fileRefNum, &count, *textHndl);
  161.             HSetState(textHndl, hstate);
  162.             if (err == eofErr) err = noErr;
  163.             if (err) count = 0;
  164.             SetHandleSize(textHndl, count);
  165.                 /* Set the handle to the actual size of the text on disk. */
  166.             (*frHndl)->doc.textHndl = textHndl;
  167.         }
  168.     }
  169.  
  170. #endif
  171.  
  172.     return(err);
  173. }
  174.  
  175.  
  176.  
  177. /*****************************************************************************/
  178.  
  179.  
  180.  
  181. /* AppWriteDocument
  182. **
  183. ** Writes the data portion of a document handle to the specified file.
  184. */
  185.  
  186. #pragma segment File
  187. OSErr    AppWriteDocument(FileRecHndl frHndl)
  188. {
  189.     short        fileRefNum;
  190.     OSErr        err;
  191. #ifdef MACSHELL_VERSION
  192.     char        hstate;
  193.     Ptr            ptr1, ptr2;
  194.     long        count, fpos;
  195. #endif
  196. #if MACSHELL_VERSION
  197.     TEHandle    te;
  198.     Handle        textHndl;
  199. #endif
  200.  
  201.     fileRefNum = (*frHndl)->fileState.refNum;
  202.  
  203.     err = SetFPos(fileRefNum, fsFromStart, 0);
  204.         /* Set the file position to the beginning of the file. */
  205.  
  206.     /* This sample code section is separated from the below section because
  207.     ** you may want to keep this sample, whereas you probably want to replace
  208.     ** the next part.  The general print and version information is read in
  209.     ** here, and the way that it is handled works well enough that you probably
  210.     ** want to keep it.  The next section however, is very application specific.
  211.     ** Also see AppReadDocument. */
  212.  
  213. #ifdef MACSHELL_VERSION
  214.  
  215.     if (!err) {        /* Write header info to file. */
  216.         hstate = LockHandleHigh((Handle)frHndl);
  217.         ptr1   = (Ptr)&((*frHndl)->doc);
  218.         ptr2   = (Ptr)&((*frHndl)->doc.endVersPrintInfo);
  219.         count  = (long)ptr2 - (long)ptr1;
  220.         err    = FSWrite(fileRefNum, &count, ptr1);
  221.         HSetState((Handle)frHndl, hstate);
  222.     }
  223.  
  224. #endif
  225.  
  226. #if MACSHELL_VERSION
  227.  
  228.     if (!err) {        /* Write out-box TextEdit control text to file. */
  229.         te       = (*frHndl)->doc.outBox;
  230.         textHndl = (*te)->hText;
  231.         count    = (*te)->teLength;
  232.         hstate   = LockHandleHigh(textHndl);
  233.         err      = FSWrite(fileRefNum, &count, *textHndl);
  234.         HSetState(textHndl, hstate);
  235.     }
  236.  
  237. #endif
  238.  
  239.     if (!err) {
  240.         err = GetFPos(fileRefNum, &fpos);
  241.         if (!err) err = SetEOF(fileRefNum, fpos);
  242.     }
  243.  
  244.     return(err);
  245. }
  246.  
  247.  
  248.  
  249. /*****************************************************************************/
  250.  
  251.  
  252.  
  253. #pragma segment File
  254. OSErr    AppDuplicateDocument(FileRecHndl oldFrHndl, FileRecHndl *newFrHndl)
  255. {
  256. #if MACSHELL_VERSION
  257.     TEHandle    te;
  258.     Handle        oldHndl, newHndl;
  259.     long        size;
  260. #else
  261. #pragma unused (oldFrHndl)
  262. #endif
  263.     OSErr        err;
  264.  
  265.     err = AppNewDocument(newFrHndl, (*oldFrHndl)->fileState.sfType);
  266.     if (!err) {
  267.  
  268. #if MACSHELL_VERSION
  269.  
  270.         te      = (*oldFrHndl)->doc.outBox;
  271.         oldHndl = (*te)->hText;
  272.         size    = (*te)->teLength;
  273.         if (newHndl = NewHandle(size)) {
  274.             (**newFrHndl)->doc.textHndl = newHndl;
  275.             BlockMove(*oldHndl, *newHndl, size);
  276.         }
  277.         else err = memFullErr;
  278.  
  279. #endif
  280.  
  281.     }
  282.  
  283.     return(err);
  284. }
  285.  
  286.  
  287.  
  288.